#include "timer.h"
#include "Tasks.h"

/**
 * @Description 定时器5 读取系统时间
 */
void Tim5_Clock_Init(void)
{
	RCC->APB1ENR |= 0x0008;	
	TIM5->CR1 = 0x0080;		//禁止计数器、使能更新、更新中断、单脉冲模式：计数器溢出后不会停止计数，递增计数、边沿对齐模式
	TIM5->CR2 = 0x0000;		//TIM5_CH1引脚连接到TI1输入
	TIM5->CNT = 0x0000;		//复位
	TIM5->ARR = 0xFFFFFFFF;	//自动重载计数值最大
	TIM5->PSC = 84 - 1;		//定时器5分频 42/84=0.5MHZ
	TIM5->EGR = 0x0001;		//定时器溢出事件       
	TIM5->CR1 |= 0x0001;	//使能定时器
}


/**
 * @Description TIM3 初始化 用于同步姿态解算任务
 */
void Tim3_Init()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  			//使能TIM3时钟 42Mhz
	
	TIM_TimeBaseInitStructure.TIM_Period = 399; 					//周期：1us*400=400us
	TIM_TimeBaseInitStructure.TIM_Prescaler = 419;  				//定时器分频 42/42=1
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; 	//向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);				//初始化TIM3
	
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); 						//允许定时器3更新中断
	TIM_Cmd(TIM3,ENABLE); 											//使能定时器3
	
	//定时器NVIC中断使能
	NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; 					//定时器3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; 		//抢占优先级2
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; 				//子优先级0
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
}


/**
 * @Description Timer4 用于同步姿态内环控制任务
 */
void Tim4_Init()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);  					//使能TIM4时钟 42mhz
	
	TIM_TimeBaseInitStructure.TIM_Period = 2499; 							//定时周期：2us*2500=5000us=5ms
	TIM_TimeBaseInitStructure.TIM_Prescaler = 83;  							//定时器分频 42/84 = 500khz
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; 			//向上计数模式
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; 
	
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);						//初始化TIM4
	
	TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); 								//允许定时器4更新中断
	TIM_Cmd(TIM4,ENABLE); 													//使能定时器4
	
	NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn; 							//定时器4中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; 				//抢占优先级0
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=2; 						//子优先级2
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStructure);	
	
}

//定时器三中断
void TIM3_IRQHandler(void)
{
	BaseType_t err;
	portENTER_CRITICAL();
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中断
	{
		TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清中断
		//同步姿态解算任务
		xSemaphoreGiveFromISR(IMU_Update,&err);
	}
	portEXIT_CRITICAL();
}


/**
 * @Description Timer4 中断服务函数 释放信号量、同步姿态内环任务
 */
void TIM4_IRQHandler(void)
{
	BaseType_t err;
	portENTER_CRITICAL();
	if(TIM_GetITStatus(TIM4,TIM_IT_Update)==SET)
	{
		TIM_ClearITPendingBit(TIM4,TIM_IT_Update); //清中断
		//同步姿态解算
		xSemaphoreGiveFromISR(AttitudeInner_Control,&err);
	}
	portEXIT_CRITICAL();
}

/**
 * @Description 读取定时器5的计数
 */
uint32_t get_micros(void)
{
 	uint32_t temp=0 ;
 	temp = TIM5->CNT;
 	return temp;
}
